/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller.Public;

import com.google.gson.Gson;
import entity.MHospital;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import session.MHospitalFacade;

/**
 *
 * @author SUNTIPARB
 */
@WebServlet(name="Public_Hospital"
            ,loadOnStartup = 1
            ,urlPatterns={"/Public/Hospital"
                        ,"/Public/Hospital/getHospital"
                        ,"/Public/Hospital/formSearch_Submit"})
public class Public_Hospital extends HttpServlet {
    @EJB
    private MHospitalFacade mHospitalFacade;
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String userPath = request.getServletPath();
        HttpSession session = request.getSession();

        // if category page is requested
        if (userPath.equals("/Public/Hospital")) {
            userPath = "/Public/Hospital";
        }

        // use RequestDispatcher to forward request internally
        String url = "/WEB-INF/view" + userPath + ".jsp";

        try {
            request.getRequestDispatcher(url).forward(request, response);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");  // ensures that user input is interpreted as
        // 8-bit Unicode (e.g., for Czech characters)
        Boolean isDispatcher = true; //กำหนดว่า ต้องการให้เปลี่ยนหน้าหรือไม่ ? ต้องการ : ไม่ต้องการ
        String userPath = request.getServletPath();
        HttpSession session = request.getSession();
        
        if (userPath.equals("/Public/Hospital/getHospital")) {
            response.setContentType("text/json;charset=utf-8");
            PrintWriter out = response.getWriter();
            List<MHospital> lMHospital;
            Map mapHospital = null;
            List listHospital = new LinkedList();
            String strJson = "";

            try {
                //Query Java Object
                Gson gson = new Gson();
                lMHospital = mHospitalFacade.LoadDataHospital("", "", "", "id", "asc");
                if (!lMHospital.isEmpty()) {
                    for (MHospital obj : lMHospital) {
                        mapHospital = new LinkedHashMap();
                        mapHospital.put("name",obj.getName());
                        mapHospital.put("location",obj.getLocation());
                        mapHospital.put("level",obj.getHospitalLevel());
                        mapHospital.put("latitude",obj.getLatitude());
                        mapHospital.put("longtitude",obj.getLongtitude());
                        mapHospital.put("id",obj.getId());
                        listHospital.add(mapHospital);
                    }
                }

                //@TODO : Add Json Value
                strJson = gson.toJson(listHospital);//JSONValue.toJSONString(listDoctor);
                out.println(strJson);
            } catch (Exception ex) {
                System.out.println("Error message" + ex.getMessage());
            } finally {
                out.close();
                lMHospital = null;
                strJson = null;
            }
            isDispatcher = false;
        }

        if (userPath.equals("/Public/Hospital/formSearch_Submit")) {
            response.setContentType("text/json;charset=utf-8");
            PrintWriter out = response.getWriter();
            String txtKeyword = (request.getParameter("txtKeyword") != null) ? request.getParameter("txtKeyword").trim() : "";
            List<MHospital> lMHospital;
            Map mapHospital = null;
            List listHospital = new LinkedList();
            String strJson = "";

            try {
                //Query Java Object
                Gson gson = new Gson();
                lMHospital = mHospitalFacade.SearchDataHospital(txtKeyword);
                if (!lMHospital.isEmpty()) {
                    for (MHospital obj : lMHospital) {
                        mapHospital = new LinkedHashMap();
                        mapHospital.put("name",obj.getName());
                        mapHospital.put("location",obj.getLocation());
                        mapHospital.put("level",obj.getHospitalLevel());
                        mapHospital.put("latitude",obj.getLatitude());
                        mapHospital.put("longtitude",obj.getLongtitude());
                        mapHospital.put("id",obj.getId());
                        listHospital.add(mapHospital);
                    }
                }

                //@TODO : Add Json Value
                strJson = gson.toJson(listHospital);//JSONValue.toJSONString(listDoctor);
                out.println(strJson);
            } catch (Exception ex) {
                System.out.println("Error message" + ex.getMessage());
            } finally {
                out.close();
                lMHospital = null;
                strJson = null;
            }
            isDispatcher = false;
        }
        
        // use RequestDispatcher to forward request internally
        String url = "/WEB-INF/view" + userPath + ".jsp";

        try {
            if (isDispatcher) {
                request.getRequestDispatcher(url).forward(request, response);
            }
        } catch (Exception ex) {
            ex.printStackTrace();

        }

    }
}
